var ns, actionsheet, toast,UIAlbumBrowser;
var app = new Vue({
    el: '#app',
    data: {
        ProjectList: [],
        ServiceTypes: [],
        selected_address_info: '',
        form: {
            id: 0,
            RoomID: 0, //资源位置ID
            FullName: '', //资源位置名称
            PhoneNo: '', //联系电话
            UserName: '', //反馈人
            Content: '', //情况说明
            AppointTime: ''
        },
        ServiceArea: '个人区域',//个人区域、公共区域
        ServiceTypeName: '', //投诉或建议
        ServiceType: 1,
        imglist: [],
        imgindex: 0,
    },
    methods: {
        get_data: function() {
            var that = this;
            ns.post({
                action: 'getmyroomsourcelist'
            }, function(succeed, data, err) {
                console.log(JSON.stringify(data));
                if (succeed) {
                    that.ProjectList = data.list;
                    that.form = data.form;
                }
            });

            // var that = this;
            // ns.post({ action: 'getmyroomsourcelist' }, function(succeed, data, err) {
            //     if (succeed) {
            //         that.ProjectList = data.list;
            //         that.form = data.form;
            //     }
            // });
        },
        onChangeRoom: function() {
            var that = this;
            for (var i = 0; i < that.ProjectList.length; i++) {
                if (that.ProjectList[i].RoomID == that.form.RoomID) {
                    that.form.PhoneNo = that.ProjectList[i].PhoneNo;
                    that.form.UserName = that.ProjectList[i].UserName;
                }
            }
        },
        do_select_area:function(type){
            var that = this;
            if (type == 1) {
                that.ServiceArea = '公共区域'
            }
            if (type == 2) {
                that.ServiceArea = '个人区域';
            }
            
            // if (type == 2 && that.form.ServiceArea == '个人区域') {
            //     setTimeout(function () {
            //         that.initPicker();//下拉数据
            //     }, 100);
            // }
        },
        // initPicker: function () {
        //     var _this = this;
        //     (function ($, doc) {
        //         $.init();
        //         $.ready(function () {
        //             var btn = document.querySelector('#choosedatetime');
        //             console.log(JSON.stringify(btn));
        //             btn.addEventListener('tap', function () {
        //                 var _self = this;
        //                 if (_self.picker) {
        //                     _self.picker.show(function (rs) {
        //                         _this.form.AppointTime = rs.value;
        //                         _self.picker.dispose();
        //                         _self.picker = null;
        //                     });
        //                 } else {
        //                     var optionsJson = this.getAttribute('data-options') || '{}';
        //                     var options = JSON.parse(optionsJson);
        //                     var id = this.getAttribute('id');
        //                     _self.picker = new $.DtPicker(options);
        //                     _self.picker.show(function (rs) {
        //                         var timer = setTimeout(function () {
        //                             clearTimeout(timer);
        //                             _this.form.AppointTime = rs.value;
        //                         }, 300);
        //                         _self.picker.dispose();
        //                         _self.picker = null;
        //                     });
        //                 }
        //             }, false);

        //         });
        //     });
        // },
        do_select_time: function () {
            var that = this;
            api.openPicker({
                type: 'date',
                date: that.form.AppointTime,
                title: '选择日期'
            }, function (ret, err) {
                if (ret) {
                    var year = ret.year;
                    var month = (ret.month >= 10 ? ret.month : '0' + ret.month);
                    var day = (ret.day >= 10 ? ret.day : '0' + ret.day);
                    api.openPicker({
                        type: 'time',
                        title: '选择时间'
                    }, function (ret2, err2) {
                        if (ret2) {
                            var hour = (ret2.hour >= 10 ? ret2.hour : '0' + ret2.hour);
                            var minute = (ret2.minute >= 10 ? ret2.minute : '0' + ret2.minute);
                            that.form.AppointTime = year + '-' +
                                month + '-' +
                                day + ' ' + hour + ':' + minute;
                        }
                    });
                }
            });
        },
        openActionsheet: function() {
            var that = this;
            ns.confirmPer('camera', function() {
                that.openActionsheetProcess();
            })
        },
        openActionsheetProcess: function() {
            var that = this;
            api.actionSheet({
                cancelTitle: '关闭',
                buttons: ['拍照', '图库选择']
            }, function(ret, err) {
                if (ret) {
                    if (ret.buttonIndex == 1) {
                        that.take_picture();
                    } else if (ret.buttonIndex == 2) {
                        that.choose_picture();
                    }
                }
            });
        },
        take_picture: function() {
            var that = this;
            api.getPicture({
                sourceType: 'camera',
                encodingType: 'jpg',
                mediaValue: 'pic',
                destinationType: 'url',
                allowEdit: true,
                quality: 100,
                saveToPhotoAlbum: false
            }, function(ret, err) {
                if (ret && ret.data) {
                    that.imglist.push({
                        index: that.imgindex,
                        url: ret.data
                    });
                    that.imgindex++;
                }
            });
        },
        // choose_picture: function() {
        //     var that = this;
        //     api.getPicture({
        //         sourceType: 'album',
        //         encodingType: 'jpg',
        //         mediaValue: 'pic',
        //         destinationType: 'url',
        //         allowEdit: true,
        //         quality: 100,
        //     }, function(ret, err) {
        //         if (ret && ret.data) {
        //             that.imglist.push({
        //                 index: that.imgindex,
        //                 url: ret.data
        //             });
        //             that.imgindex++;
        //         }
        //     });
        // },
        choose_picture: function () {
            var that = this;
            ns.confirmPer('camera', function () {
                that.choose_picture_start();
            })
        },
        choose_picture_start: function () {
            var that = this;
            if (that.imglist.length >= that.maxuploadcount) {
                api.toast({
                    msg: '最多支持' + that.maxuploadcount + '张图片',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            UIAlbumBrowser.imagePicker({
                max: that.maxuploadcount - that.imglist.length,
                styles: {
                    bg: '#000000',
                    mark: {
                        position: 'top_right',
                        size: 20
                    },
                    nav: {
                        bg: '#000000',
                        cancelColor: '#fff',
                        cancelSize: 16,
                        nextStepColor: '#7fff00',
                        nextStepSize: 16
                    },
                    thumbnail: { //（可选项）返回的缩略图配置，**建议本图片不要设置过大** 若已有缩略图，则使用已有的缩略图。若要重新生成缩略图，可先调用清除缓存接口api.clearCache()。
                        w: 100, //（可选项）数字类型；返回的缩略图的宽；默认：原图的宽度
                        h: 100 //（可选项）数字类型；返回的缩略图的宽；默认：原图的高度
                    }
                },
                animation: true,
            }, function (ret) {
                if (ret.eventType == 'nextStep') {
                    if (ret.list && ret.list.length > 0) {
                        that.select_imglist = ret.list;
                        that.select_imgindex = 0;
                        that.UIAlbumBrowser_transPath();
                    }
                    UIAlbumBrowser.closePicker();
                }
                if (ret.originalPath && ret.originalPath.length > 0) {
                    that.imglist.push({
                        index: that.imgindex,
                        url: ret.originalPath,
                        thumbPath: ret.originalPath
                    });
                    that.imgindex++;
                }
            });
            return;
        },
        UIAlbumBrowser_transPath: function () {
            var that = this;
            var item = that.select_imglist[that.select_imgindex];
            UIAlbumBrowser.transPath({
                path: item.path
            }, function (ret, err) {
                if (ret) {
                    that.select_imgindex++;
                    if (that.select_imgindex < that.select_imglist.length) {
                        that.UIAlbumBrowser_transPath();
                    } else {
                        that.select_imgindex = 0;
                    }
                    that.imglist.push({
                        index: that.imgindex,
                        url: ret.path,
                        thumbPath: item.thumbPath
                    });
                    that.imgindex++;
                }
            });
        },






        remove_img: function(index) {
            var that = this;
            api.confirm({
                title: '提示',
                msg: '确定删除?',
                buttons: ['确定', '取消']
            }, function(ret, err) {
                if (ret.buttonIndex == 1) {
                    for (var i = 0; i < that.imglist.length; i++) {
                        var item = that.imglist[i];
                        if (item.index == index) {
                            that.imglist.splice(i, 1);
                        }
                    }
                }
            });
        },
        open_hotline: function() {
            var that = this;
            ns.post({
                action: 'getmallhotline'
            }, function(succeed, data, err) {
                if (succeed) {
                    api.call({
                        type: 'tel_prompt',
                        number: data.hotline
                    });
                }
            });
        },
        do_open_add: function() {
            var that = this;
            if (that.form.RoomID == 0) {
                api.toast({
                    msg: '请选择房间',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (that.form.PhoneNo == '') {
                api.toast({
                    msg: '请填写联系电话',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (that.form.UserName == '') {
                api.toast({
                    msg: '请填写报修人',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (that.form.Content == '') {
                api.toast({
                    msg: '请填写情况说明',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            this.do_save();
        },
        do_save: function() {
            var that = this;
            var filelist = [];
            for (var i = 0; i < that.imglist.length; i++) {
                filelist.push(that.imglist[i].url);
            }
            var options = {};
            options.RoomID = that.form.RoomID;
            options.FullName = that.form.FullName;
            options.PhoneNo = that.form.PhoneNo;
            options.Content = that.form.Content;
            options.UserName = that.form.UserName;
            options.ServiceArea = that.ServiceArea;
            options.AppointTime = that.form.AppointTime;
            options.ServiceType = that.ServiceType; //投诉及建议
            options.action = 'savenewbaoxiujianyi';
            toast.loading({
                title: "提交中",
                duration: 2000
            }, function (ret) { });
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    that.form.Content = '';
                    that.form.AppointTime = '';
                    that.form.ServiceContent = '';
                    that.imglist = [];
                    api.toast({
                        msg: '提交成功',
                        duration: 2000,
                        location: 'bottom'
                    });
                    api.closeWin();
                    api.sendEvent({
                        name: 'reload_servicelist',
                    });
                    
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                files: {
                    file: filelist
                },
                toast: true,
                toastmsg: '提交中'
            });
        },
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    toast = new auiToast();
    actionsheet = new auiActionsheet();
    UIAlbumBrowser = api.require('UIAlbumBrowser');
    app.form.id = api.pageParam.id || 0;
    app.get_data();
};
